import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
@WebServlet(urlPatterns={"/muiltLang","/ajaxmethod","/userService/*"},loadOnStartup=1)
public class MuiltlangSerlvet  extends HttpServlet{
	
   Logger  logger=LoggerFactory.getLogger(MuiltlangSerlvet.class);
	private static final long serialVersionUID = 6677360505110618758L;
	 @Override
	public void init(ServletConfig config) throws ServletException {
		 System.out.println("----------muiltLang------------------");
	}
	 
	 /**
	  * 国家化的配置文件仅仅一次项目运行周期内 加载一次
	  * 命名规范：  lang_en_US.properties  前缀名称_language_country.propertis
	  * 
	  * 需要动态修改时候---重启项目
	  */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		      String requestUrl=   request.getRequestURI();
		 if(requestUrl.indexOf("userService/toAddUser")>=0){
			 try {
				new Thread().sleep(1400);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    request.getRequestDispatcher("/WEB-INF/ajax/dataAddUser.jsp").forward(request, response);	 
		 }
		 else if(requestUrl.indexOf("userService/addUser")>=0){
			String name=  request.getParameter("name");
			String password=  request.getParameter("password");
			logger.info(String.format("用户名：%s 密码为：%s",name, password));
			  Map<String ,Object> map=new HashMap<String ,Object>();
			  map.put("flag", 1);
			  map.put("mes", "对不起保存失败");
			 String ret=  new Gson().toJson(map);
			 toClient(ret, response);
		 }
		 else{
			  if(request.getParameter("method").equals("muilt"))   
			       muiltLangMethod(request, response);
		      else if(true){
		    	 System.out.println(request.getParameter("name"));
		    	/* People    people= new  People();
		    	 people.setName(request.getParameter("name"));
		    	 people.setBirthday("2000-01-01");
		    	 people.setSex("未知");*/
		        //String ret=  new Gson().toJson(people);
		        //toClient(ret, response);
		        System.out.println("-----ajax post----");
		       // request.setAttribute("people", people);
		    request.getRequestDispatcher("/WEB-INF/ajax/dataJson.jsp").forward(request, response);	 
		 }
		 }
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	
	
	public void  muiltLangMethod(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
		Locale locale = request.getLocale();
	       if(request.getParameter("abc").toString().equals("zh")){
	    	   locale=new Locale("zh", "CN");
	         }
	       else if(request.getParameter("abc").toString().equals("en")){
	    	   locale=new Locale("en", "US");
	         }
 //  System.out.println(locale.toString());
      //需要使用util包中的  ResourceBundle
	       ResourceBundle resb1 = ResourceBundle.getBundle("lang", locale); 
      System.out.println(resb1.getString("username"));
      System.out.println(   resb1.getString("password"));
      request.setAttribute("username", resb1.getString("username"));
      request.setAttribute("password", resb1.getString("password"));
         request.getRequestDispatcher("/mulitLang.jsp").forward(request, resp);
		
		
	}
	
	/**
	  * 向客户端放会固定格式的JSON字符串
	  * @param ret
	  * @param response
	  */
	public static void  toClient(Object ret,HttpServletResponse response){
		PrintWriter out = null;
		try {
			response.setContentType("application/json; charset=utf-8");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			out.println(ret);
		} catch (IOException e) {
			e.printStackTrace();

		}finally{
			if(out != null){
				out.flush();
				out.close();
				out = null;
			}
		}
	}
}
